package com.chenjian.designpattern.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author TC2952
 */
public class JdkDynamicProxy implements InvocationHandler {

    private Person person;

    public JdkDynamicProxy(Person person) {
        this.person = person;
    }

    public Object getTarget(){

        Object instance = Proxy.newProxyInstance(person.getClass().getClassLoader(), person.getClass().getInterfaces(), this);

        return instance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("前置增强");
        //原方法执行
        Object invoke = method.invoke(person, args);
        System.out.println("后置增强");

        return invoke;
    }
}
